<div align="center"><h1>Add a new protocol</h1></div>
<form name="Add" method="post" action="addprotocol.php">
<table width="40%" style="margin-left: 30%" border="1" cellspacing="1" cellpadding="0">
<tr>
	<td>
		<table width="100%" border="0" cellspacing="1" cellpadding="0">
			<tr>
				<td align="center"><strong>Name</strong></td>
				<td align="center"><strong>Port</strong></td>
			</tr>
			<tr>
				<td align="center">
				<select name="Name">
				<?php
					require_once('config.php');
					foreach($protocols as $protocol){
						echo "<option value=\"$protocol\">$protocol</option>"; 
					} 
				?>				
				</select>
				</td>
				<td align="center"><input name="Port" type="text" id="Port"></td>
			</tr>
			<tr>
				<td align="center"><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"></td>
				<td align="center" colspan="2"><input type="submit" name="Submit" value="Submit"></td>
			</tr>
		</table>
	</td>
</tr>
</table>
</form>
<div align="center"><?php if(isset($_GET['message'])) echo $_GET['message'];?></div><br />
<div align="center"><a href="protocols.php">Back to Protocol Management</a></div>
<div align="center"><a href="index.php">Back to Main Menu</a></div>"
<?php
if(isset($_POST['Submit'])){
	require_once('config.php');
	require_once('db.php');
	$sql = "INSERT INTO protocols(name, port) VALUES(";	
	if (isset($_POST['Name']))
	{
		$Name = $_POST['Name'];
		if(trim($Name) === ''){ 
			$message .= "Protocol name should not be empty. ";
			header("Location: addprotocol.php?message=" . urlencode($message));
			exit;
		} 
		$sql .= "'$Name', ";
		
	}else{
		$message .= "Don't have any protocol specified";
		header("Location: addprotocol.php?message=" . urlencode($message));
		exit;
	}	
	if (isset($_POST['Port']))
	{
		$Port = $_POST['Port'];
		if($Port == '') $Port = 0;
		$sql .= $Port . ")";
	}else{
		$message .= "Don't have any protocol port specified";
		header("Location: addprotocol.php?message=" . urlencode($message));
		exit;
	}
	$link = connectdb($servername,$dbname,$dbusername,$dbpassword);
	$rt = mysql_query($sql, $link);
	if($rt){
		$message = urlencode("<b>Add successfully</b>");
		close($link);
		header("Location: protocols.php?message=" . $message);
						
	}
	else {
		$message = urlencode("There is an MySQL error occurred: <b>" . mysql_error($link) . "</b>");
		close($link);
		header("Location: addprotocol.php?message=" . $message);		
	}
	
	
}
	
 ?>